package com.ruoyi.ruoyi.business.service.smo.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.pojo.entity.Category;
import com.ruoyi.common.pojo.entity.Tutorial;
import com.ruoyi.common.pojo.service.CategoryService;
import com.ruoyi.common.pojo.service.TutorialService;
import com.ruoyi.ruoyi.business.pojo.dto.*;
import com.ruoyi.ruoyi.business.pojo.vo.MaterialDetailVo;
import com.ruoyi.ruoyi.business.pojo.vo.MaterialListClassifyVo;
import com.ruoyi.ruoyi.business.pojo.vo.MaterialListVo;
import com.ruoyi.ruoyi.business.service.smo.MaterialService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * ClassName：MaterialServiceImpl
 * Package：com.ruoyi.ruoyi.business.service.smo.impl
 * Description：
 *
 * @Author：fpy
 * @Create：2025/5/8 - 14:39
 * @Version：v1.0
 */
@Service
public class MaterialServiceImpl implements MaterialService {
    @Resource
    private TutorialService tutorialService;
    @Resource
    private CategoryService categoryService;

    @Override
    public AjaxResult<Page<MaterialListVo>> list(Page dtoPage, MaterialListDto dto) {
        Page<Tutorial> page = tutorialService.page(dtoPage,
                Wrappers.<Tutorial>lambdaQuery()
                        .like(StrUtil.isNotBlank(dto.getName()), Tutorial::getName, dto.getName())
                        .eq(StrUtil.isNotBlank(dto.getCategory_id()), Tutorial::getCategoryId, dto.getCategory_id())
                        .orderByDesc(Tutorial::getSort, Tutorial::getUpdateTime)
        );

        Page<MaterialListVo> pageVo = new Page<>();
        pageVo.setTotal(page.getTotal());
        pageVo.setSize(page.getSize());
        pageVo.setCurrent(page.getCurrent());
        pageVo.setRecords(
                Optional.ofNullable(page.getRecords())
                        .orElseGet(ArrayList::new)
                        .stream()
                        .map(t -> {
                            MaterialListVo vo = new MaterialListVo();
                            vo.setId(t.getId());
                            vo.setType(t.getType());
                            vo.setCategory_id(t.getCategoryId());
                            vo.setCategory_name(t.getCategoryName());
                            vo.setGrade(t.getGrade());
                            vo.setName(t.getName());
                            vo.setSort(t.getSort());
                            vo.setUpdate_time(t.getUpdateTime());
                            vo.setDesc(t.getRemark());

                            return vo;
                        })
                        .collect(Collectors.toList())
        );

        return AjaxResult.success(pageVo);
    }

    @Override
    public AjaxResult add(MaterialAddDto dto) {
        Category category = categoryService.getById(dto.getCategory_id());
        if (category == null) {
            return AjaxResult.error("请选择分类");
        }

        Tutorial tutorial = new Tutorial();
        tutorial.setType(dto.getType());
        tutorial.setCategoryId(dto.getCategory_id());
        tutorial.setCategoryName(category.getName());
        tutorial.setGrade(dto.getGrade());
        tutorial.setName(dto.getName());
        tutorial.setCoverImage(dto.getCover_image());
        tutorial.setContent(dto.getContent());
        tutorial.setLink(dto.getLink());
        tutorial.setImgUrl(dto.getImg_url());
        tutorial.setVideoUrl(dto.getVideo_url());
        tutorial.setSort(dto.getSort());
        tutorial.setShowFlag(dto.getShow_flag());
        tutorial.setRemark(dto.getDesc());
        tutorialService.save(tutorial);

        return AjaxResult.success();
    }

    @Override
    public AjaxResult remove(MaterialRemoveDto dto) {
        tutorialService.removeById(dto.getId());

        return AjaxResult.success();
    }

    @Override
    public AjaxResult<MaterialDetailVo> detail(MaterialDetailDto dto) {
        Tutorial tutorial = tutorialService.getById(dto.getId());
        if (tutorial == null) {
            return AjaxResult.error("教程不存在");
        }

        MaterialDetailVo vo = new MaterialDetailVo();
        vo.setId(tutorial.getId());
        vo.setType(tutorial.getType());
        vo.setCategory_id(tutorial.getCategoryId());
        vo.setGrade(tutorial.getGrade());
        vo.setName(tutorial.getName());
        vo.setCover_image(tutorial.getCoverImage());
        vo.setContent(tutorial.getContent());
        vo.setLink(tutorial.getLink());
        vo.setImg_url(tutorial.getImgUrl());
        vo.setVideo_url(tutorial.getVideoUrl());
        vo.setSort(tutorial.getSort());
        vo.setShow_flag(tutorial.getShowFlag());
        vo.setDesc(tutorial.getRemark());

        return AjaxResult.success(vo);
    }

    @Override
    public AjaxResult edit(MaterialEditDto dto) {
        Category category = categoryService.getById(dto.getCategory_id());
        if (category == null) {
            return AjaxResult.error("请选择分类");
        }

        Tutorial tutorial = new Tutorial();
        tutorial.setId(dto.getId());
        tutorial.setType(dto.getType());
        tutorial.setCategoryId(dto.getCategory_id());
        tutorial.setCategoryName(category.getName());
        tutorial.setGrade(dto.getGrade());
        tutorial.setName(dto.getName());
        tutorial.setCoverImage(dto.getCover_image());
        tutorial.setContent(dto.getContent());
        tutorial.setLink(dto.getLink());
        tutorial.setImgUrl(dto.getImg_url());
        tutorial.setVideoUrl(dto.getVideo_url());
        tutorial.setSort(dto.getSort());
        tutorial.setShowFlag(dto.getShow_flag());
        tutorial.setRemark(dto.getDesc());
        tutorialService.updateById(tutorial);

        return AjaxResult.success();
    }

    @Override
    public AjaxResult<List<MaterialListClassifyVo>> listClassify() {
        List<Category> list = categoryService.list(
                Wrappers.<Category>lambdaQuery()
                        .select(Category::getId, Category::getName)
                        .orderByAsc(Category::getSort)
        );

        List<MaterialListClassifyVo> voList = list.stream()
                .map(c -> {
                    MaterialListClassifyVo vo = new MaterialListClassifyVo();
                    vo.setId(c.getId());
                    vo.setName(c.getName());

                    return vo;
                })
                .collect(Collectors.toList());

        return AjaxResult.success(voList);
    }
}
